package net.tv90.bfmccore.listener;

import net.tv90.bfmccore.game.GameManager;
import net.tv90.bfmccore.game.view.GameModelViewHandler;
import net.tv90.bfmccore.helper.FactionHelper;
import net.tv90.bfmccore.listener.effect.NeedSuppEffect;
import net.tv90.bfmccore.listener.effect.RadioEffect;
import net.tv90.bfmccore.listener.effect.SimpleRadioEffect;
import net.tv90.bfmccore.listener.effect.SpotEffect;
import net.tv90.bfmccore.util.Cooldown;
import org.bitterorange.flansmodslain.actions.Point;
import org.bitterorange.flansmodslain.models.BFMCBukkitKeyInputEvent;
import org.bitterorange.flansmodslain.models.PlayerSlainEvent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;

import java.util.HashMap;

import static net.tv90.bfmccore.helper.FactionHelper.*;
import static net.tv90.bfmccore.helper.MCHHelper.getMCHEntitiesSoundFilterType;
import static org.bitterorange.flansmodslain.FlansModSlain.getLinkedKillCountOf;

public class RadioListener implements Listener {

    static HashMap<String, RadioEffect> radioMap = new HashMap<>();

    static {
        //通用
        radioMap.put("/radio accept", new SimpleRadioEffect("ACCEPT", "好的"));
        radioMap.put("/radio deny", new SimpleRadioEffect("DENY", "拒绝"));
        radioMap.put("/radio needammo", new SimpleRadioEffect("NEEDAMMO", "需要弹药补给！"));
        radioMap.put("/radio needheal", new SimpleRadioEffect("NEEDHEAL", "需要医疗！"));
        radioMap.put("/radio needsupp", new NeedSuppEffect("NEEDSUPP", "需要火力支援！"));
        radioMap.put("/radio needrepair", new SimpleRadioEffect("NEEDREPAIR", "需要维修！"));
        radioMap.put("/radio needpickup", new SimpleRadioEffect("NEEDPICKUP", "需要载具接送！"));
        radioMap.put("/radio thank", new SimpleRadioEffect("THANK", "谢谢你！"));
        radioMap.put("/radio spot", new SpotEffect("SPOT", "发现敌军"));
        radioMap.put("/radio follow", new SimpleRadioEffect("FOLLOW", "跟我来！"));
        //小队长
        radioMap.put("/radio gogogo", new SimpleRadioEffect("GOGOGO", "前进！"));
        radioMap.put("/radio attack", new SimpleRadioEffect("ATTACK", "攻击这个目标！"));
        radioMap.put("/radio defend", new SimpleRadioEffect("DEFEND", "防守这个目标！"));
        radioMap.put("/radio needscan", new SimpleRadioEffect("NEEDSCAN", "请求卫星搜索！"));
        radioMap.put("/radio needsupply", new SimpleRadioEffect("NEEDSUPPLY", "请求补给投放！"));
        radioMap.put("/radio needvehicle", new SimpleRadioEffect("NEEDVECHCLE", "请求车辆投放！"));
        radioMap.put("/radio needartillery", new SimpleRadioEffect("NEEDARTILLERY", "请求炮击！"));
    }

    @EventHandler
    public void newPlayerButtonEvent(BFMCBukkitKeyInputEvent e) {
        Player p = e.getPlayer();
        switch (e.getKeyType()) {
            case menu: {
                if (!GameModelViewHandler.vexViewVerificationPlayers.contains(p.getName()) || GameManager.inBattleFieldPlayers.contains(p)) return;
                try {
                    GameModelViewHandler.getInstance().createMainGUI(p);
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                break;
            }
            case f10: {
                if(Cooldown.isCool("f10", p)) {
                    return;
                }
                String vehicle = getMCHEntitiesSoundFilterType(p);
                for (Player allPlayer : Bukkit.getOnlinePlayers()) {
                    if (p.getUniqueId() != allPlayer.getUniqueId()) {
                        allPlayer.playSound(allPlayer.getLocation(), "filter." + vehicle + "_FILTER", 100.0F, 1.0F);
                        allPlayer.playSound(allPlayer.getLocation(), "f10.F10", 100.0F, 1.0F);
                    }
                }
                p.playSound(p.getLocation(), "f10.F10", 100.0F, 1.0F);
                Cooldown cd = new Cooldown("f10", 2, p);
                cd.startForSinglePlayer();
                break;
            }
            case taunt: {
                if(Cooldown.isCool("taunt", p)) {
                    return;
                }
                String vehicle = getMCHEntitiesSoundFilterType(p);
                String country = FactionHelper.getCountry2(p);
                for (Player allPlayer : Bukkit.getOnlinePlayers()) {
                    if (p.getUniqueId() != allPlayer.getUniqueId()) {
                        allPlayer.playSound(allPlayer.getLocation(), "filter." + vehicle + "_FILTER", 100.0F, 1.0F);
                        allPlayer.playSound(allPlayer.getLocation(), "taunt." + country, 100.0F, 1.0F);
                    }
                }
                p.playSound(p.getLocation(), "taunt." + country, 100.0F, 1.0F);
                Cooldown cd = new Cooldown("taunt", 4, p);
                cd.startForSinglePlayer();
                break;
            }
        }
    }

    @EventHandler
    public void setPlayerChatFormat(AsyncPlayerChatEvent e) {
        Player player = e.getPlayer();
        e.setFormat(ChatColor.RESET + "[" + Point.getRankOf(player) + ChatColor.RESET + " " + Point.get(player) + "] " + ChatColor.RESET + "%1$s" + ChatColor.BLUE + ChatColor.BOLD + " >>> " + ChatColor.RESET + "%2$s");
    }

    @EventHandler
    public void playerChat(PlayerCommandPreprocessEvent e) {
        Player p = e.getPlayer();
        String country = getCountry2(p);
        String soundFilterType = getMCHEntitiesSoundFilterType(p);
        String message = e.getMessage();
        if(radioMap.containsKey(message)) {
            RadioEffect effect = radioMap.get(message);
            effect.playEffectForPlayer(p, country);
            for (Player teammates : getTeammates(p)) {
                effect.playEffectForTeamExcludeSelf(p, teammates, country, soundFilterType);
            }
            for (Player teamer : getTeamer(p)) {
                effect.playEffectForTeam(p, teamer);
            }
        }
    }

    @EventHandler
    public void playerSlain(PlayerSlainEvent e) {
        if(!e.isKilledAI() && !e.isKillerAI()){
            Player killer = e.getPlayerKiller();
            Player killed = e.getPlayerKilled();
            int killedLinkedKillCount = getLinkedKillCountOf(killed);
            if (killedLinkedKillCount > 3) {
                killed.playSound(killed.getLocation(), "faction." + getCountry2(killed) + "_SCREAM", 20.0F, 1.0F);
            }
        }
    }

    @EventHandler
    public void playerDamage(EntityDamageEvent e) {
        if (e.getEntity() instanceof Player) {
            Player p = (Player) e.getEntity();
            int link = getLinkedKillCountOf(p);
            if (link > 3 && p.getHealth() <= 6.0) {
                p.playSound(p.getLocation(), "faction." + getCountry2(p) + "_SCREAM", 20.0F, 1.0F);
            }
        }
    }


    @EventHandler
    public void playerDeath(PlayerDeathEvent e) {
        Player p = e.getEntity();
        p.playSound(p.getLocation(), "game.player.die", 4.0F, 1.0F);
        if (!p.isOp()) {
            try {
                p.setOp(true);
                p.performCommand("minecraft:clear");
            } finally {
                p.setOp(false);
            }
        } else {
            p.performCommand("minecraft:clear");
        }
    }

    @EventHandler
    public void playerInteract(PlayerInteractEvent e) {
        Player p = e.getPlayer();
        ItemStack isk = e.getItem();
        if (p.getFireTicks() > 0) {
            if (isk != null) {
                String name = isk.getType().name();
                if (name.equals("FLANSMOD_BANDAGE") || name.equals("FLANSMOD_MEDKIT") || name.equals("FLANSMOD_FIRSTAIDPACK")) {
                    p.setFireTicks(0);
                }
            }
        }
    }

    @EventHandler
    public void playerCommand(PlayerCommandPreprocessEvent e) {
        Player player = e.getPlayer();
        String message = e.getMessage();
        if (message.equalsIgnoreCase("/suicide")) {
            player.playSound(player.getLocation(), "flansmod:die", 4.0F, 1.0F);
        }
    }
}